#include <iostream>

#define INT_MAX 10000
using namespace std;

int d[8][8],s[8][8];


void init(){
    int i,j;
    for(i=0;i<8;i++){
        for(j=0;j<8;j++){
            d[i][j] = INT_MAX;
            if(i!=j)
                s[i][j] = j;
        }
    }

    d[0][1] = 3;
    d[0][2] = 8;
    d[0][4] = -4;
    d[1][4] = 7;
    d[1][3] = 1;
    d[2][1] = 4;
    d[3][2] = -5;
    d[3][0] = 2;
    d[4][3] = 6;
}

void echo(){
    int i,j;
    for(i=0;i<5;i++){
        for(j=0;j<5;j++){
            cout << d[i][j] << "\t";
        }
        cout << "\n";
    }
    cout << "\n";
    for(i=0;i<5;i++){
        for(j=0;j<5;j++){
            cout << s[i][j] << "\t";
        }
        cout << "\n";
    }
    cout << "\n";
}

int main(){
    int i,j,k;
    init();
       
    for(i=0;i<5;i++){
        for(j=0;j<5;j++){
            for(k=0;k<5;k++){
                if(d[j][k] > d[j][i]+d[i][k] && j != k ){    
                    d[j][k] = d[j][i]+d[i][k];
                    s[j][k] = i;
                }
            }
        }
    }

    echo();

}